package com.warmlight.voicepacket.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.umeng.analytics.MobclickAgent;
import com.warmlight.voicepacket.PlayDeatalActivity;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.data.AllplayerData;
import com.warmlight.voicepacket.data.PlaydeatalData;
import com.warmlight.voicepacket.data.eventbus.ShareUnlock;
import com.warmlight.voicepacket.fragment.CustomBottomSheetDialogFragment;
import com.warmlight.voicepacket.http.HttpClient;
import com.warmlight.voicepacket.http.HttpConfig;
import com.warmlight.voicepacket.utils.ClickLog;
import com.warmlight.voicepacket.utils.GlideCornerTransform;
import com.warmlight.voicepacket.utils.JSONUtils;
import com.warmlight.voicepacket.utils.MobClickHelper;
import com.warmlight.voicepacket.utils.SPUtils;
import com.warmlight.voicepacket.utils.ToastUtil;
import com.zhuge.analysis.stat.ZhugeSDK;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import okhttp3.Call;

import static com.warmlight.voicepacket.db.VoicePacketDBHelper.LIST_ID;

public class AllPlayCoverRCAdapter extends RecyclerView.Adapter<AllPlayCoverRCAdapter.MyHolder> {
    private List<AllplayerData.DataBean.ListBean> mList;
    private Context mContext;
    private String type = "";//鉴别s p
    private final RequestOptions options;

    private String pushtitle = "";
    private String category_id;
    public AllPlayCoverRCAdapter(Context context,List<AllplayerData.DataBean.ListBean> datas,
                                 String type,String pushtitle,String category_id){
        this.mContext = context;
        this.mList = datas;
        this.type = type;
        if (this.type == null){
            this.type = "";
        }
        options = new RequestOptions();
        options.transform(new GlideCornerTransform(mContext,5));
        this.pushtitle = pushtitle;
        if (this.pushtitle == null){
            this.pushtitle = "";
        }
        this.category_id = category_id;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_allplayer_item, null);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        String isShareRight = "";
        String isUnlockedRight = "";

        isShareRight =  mList.get(position ).getIs_share();
        isUnlockedRight = mList.get(position).getIs_unlocked();


        final String isShareLeft = ((AllplayerData.DataBean.ListBean) mList.get(position)).getIs_share();
        final String isUnlockedLeft = ((AllplayerData.DataBean.ListBean) mList.get(position )).getIs_unlocked();

        holder.rl_item_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("1".equals(isShareLeft) && "0".equals(isUnlockedLeft)){//未解锁
                    AllplayerData.DataBean.ListBean.ShareBean share = ((AllplayerData.DataBean.ListBean) mList.get(position)).getShare();
                    PlaydeatalData.DataBean.ShareBean shareBean = new PlaydeatalData.DataBean.ShareBean();
                    shareBean.setDesc(share.getDesc());
                    shareBean.setH5_url(share.getH5_url());
                    shareBean.setPic_url(share.getPic_url());
                    shareBean.setTitle(share.getTitle());
                    new CustomBottomSheetDialogFragment(shareBean, new CustomBottomSheetDialogFragment.ShareCallBack() {
                        @Override
                        public void shareSuccess(boolean isSuccess) {
                            shareResult(isSuccess,((AllplayerData.DataBean.ListBean) mList.get(position)).getId(),position);
                        }
                    },true).show(((FragmentActivity)mContext).getSupportFragmentManager(), "listshare");
                }else {
                    Intent intent = new Intent(mContext, PlayDeatalActivity.class);
                    intent.putExtra(LIST_ID, ((AllplayerData.DataBean.ListBean) (mList.get(position ))).getId());
                    intent.putExtra("type",type);
                    mContext.startActivity(intent);
                }

                String id = ((AllplayerData.DataBean.ListBean) (mList.get(position ))).getId();
                String name = ((AllplayerData.DataBean.ListBean) (mList.get(position ))).getName();
                if ("push".equals(type)){
                    MobclickAgent.onEvent(mContext, MobClickHelper.p_bag_click,name+"-"+pushtitle+"-"+ SPUtils.getLoginUid());
                    HashMap<String, Object> temp = new HashMap<>();
                    temp.put("点击通知", name);
                    ZhugeSDK.getInstance().track(mContext, MobClickHelper.p_bag_click, temp);
                    ClickLog.clickLog(ClickLog.p_bag_all_rec, id, "0",category_id);
                    ClickLog.clickLog(ClickLog.p_bag_all_rec, id, "0",category_id);
                }else {
                    ClickLog.clickLog(ClickLog.bag_all_rec, id, "0",category_id);
                }
            }
        });
        final String finalIsShareRight = isShareRight;
        final String finalIsUnlockedRight = isUnlockedRight;
        holder.tv_header_name_left.setText(((AllplayerData.DataBean.ListBean)(mList.get(position))).getName());
        if (options != null) {
            Glide.with(mContext).load(((AllplayerData.DataBean.ListBean) (mList.get(position))).getPic()).apply(options).into(holder.iv_header_bg_left);
        }
        if ("1".equals(isShareLeft) && "0".equals(isUnlockedLeft)) {//未解锁
            holder.iv_lock_left.setVisibility(View.VISIBLE);
        }else{
            holder.iv_lock_left.setVisibility(View.GONE);
        }
    }

    public void refresh(String coverId){
        for (int i=0;i<getItemCount();i++){
            AllplayerData.DataBean.ListBean item = (AllplayerData.DataBean.ListBean) mList.get(i);
            if (coverId.equals(item.getId())){
                item.setIs_unlocked("1");
            }
        }
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyHolder extends RecyclerView.ViewHolder{
        RelativeLayout rl_item_left ;
//        RelativeLayout rl_item_right;
        TextView tv_header_name_left;
//        TextView tv_header_name_right;
        ImageView iv_header_bg_left;
//        ImageView iv_header_bg_right;
        ImageView iv_lock_left;
//        ImageView iv_lock_right;
        public MyHolder(View itemView) {
            super(itemView);
//            rl_item_right =  itemView.findViewById(R.id.rl_item_right);

//            tv_header_name_right =  itemView.findViewById(R.id.tv_header_name_right);

//            iv_header_bg_right =  itemView.findViewById(R.id.iv_header_bg_right);
            rl_item_left =  itemView.findViewById(R.id.rl_item_left);
            tv_header_name_left =  itemView.findViewById(R.id.tv_header_name_left);
            iv_header_bg_left =  itemView.findViewById(R.id.iv_header_bg_left);
            iv_lock_left =  itemView.findViewById(R.id.iv_lock_left);
//            iv_lock_right =  itemView.findViewById(R.id.iv_lock_right);
        }
    }
    private void shareResult(boolean isSuccess, final String id, final int position){
        if (isSuccess) {
            TreeMap<String,String> map = new TreeMap<>();
            map.put("audio_id",id);
            map.put("type","2");
            HttpClient.post(HttpConfig.SERVER + HttpConfig.SHARE, map, new com.warmlight.voicepacket.http.Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    ((Activity)mContext).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.showShort(mContext,"分享失败");
                        }
                    });
                }

                @Override
                public void onResponse(Call call, String response){
//                    String string = response.body().string();
                    ArrayList<Map<String, String>> listMapByJson = JSONUtils.getListMapByJson(response);
                    if (listMapByJson.size()>0){
                        Map<String, String> map1 = listMapByJson.get(0);
                        if ("200".equals(map1.get("code"))){
                            ((AllplayerData.DataBean.ListBean)(mList.get(position))).setIs_share("0");
                            ((AllplayerData.DataBean.ListBean)(mList.get(position))).setIs_unlocked("1");
                            ((Activity)mContext).runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    notifyDataSetChanged();
                                    EventBus.getDefault().post(new ShareUnlock(false,position,id));
                                }
                            });
                        }
                    }
                }
            });
        }
    }
}
